<template>
  <view id="productID">
    <!-- <view>
      <TableTitle :title="'出厂模拟数据'" @click="openVideo"></TableTitle>
    </view> -->
    <view class="tableContentClass">
      <transition-group name="fade" tag="ul">
        <template v-for="(item, index) in dataLists" :key="index">
          <view class="tableClass" v-if="!item.isReplacing">
            <li>{{ index + 1 }}</li>
            <li>{{ item.code }}</li>
            <!-- <li>{{ formatDate(item.last_select_date, 'YY年MM月DD') }}</li> -->
          </view>
        </template>
      </transition-group>
      <view class="pageClass" style="color: #fff !important">共{{ dataLists.length }}条记录</view>
    </view>
    <view class="dataContentClass">
      <div class="shengchan">
        <div>实时生产</div>
        <div>{{ shengchan }}万桶</div>
      </div>
      <div class="hexiao">
        <div>实时核销</div>
        <div>{{ hexiao }}万桶</div>
      </div>
    </view>
  </view>
</template>

<script setup lang="ts">
import TableTitle from '../TableTitle/TableTitle.vue'
import { formatDate, timeNumber, timeDiff } from '@/utils/days'
import {
  getAllDataAPI,
  getShuiAllDataAPI,
  getTeaDataAPI,
  getpackagesDataAPI,
  getclothesDataAPI,
  getshoesDataAPI,
  getlangDataAPI,
  getmilkDataAPI,
  getLiBangDataAPI,
} from '@/services/allData'
import { useyanZhengStore } from '@/stores/modules/yanZheng'
import { useWebsocketStore } from '@/stores/modules/websocket'
import { useVideoStore } from '@/stores/modules/video'
import { v4 as uuidv4 } from 'uuid'

// ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'

const yanZhengStore = useyanZhengStore()
const dataLists = ref(new Array())

function aotuAddData() {
  let oldUUID = uuidv4().split('-')
  let newUUID = (oldUUID[0] + '********' + oldUUID[oldUUID.length - 1]).toUpperCase()
  // console.log(newUUID)
  dataLists.value.push({ code: newUUID, isReplacing: false })
}

function aotuDelData() {
  let index = Math.floor(Math.random() * 10)
  // console.log(index)

  // 标记为正在替换
  dataLists.value[index].isReplacing = true

  setTimeout(() => {
    // 移除旧项，并在同一位置添加新项
    dataLists.value.splice(index, 1)
    nextId.value++
  }, 500) // 确保这个延时与.fade-leave-active的持续时间一致
}

setInterval(() => {
  if (dataLists.value.length < 10) aotuAddData()
}, 2000)

setInterval(() => {
  if (dataLists.value.length >= 10) aotuDelData()
}, 5000)

const shengchan = ref(43267)
const hexiao = ref(39856)

setInterval(() => {
  shengchan.value++
}, 2000)

setInterval(() => {
  hexiao.value++
}, 5000)

//#region
// async function getData() {
//   // const res: any = await getAllDataAPI() //获取茅台数据
//   const res: any = await getShuiAllDataAPI() //获取水数据
//   // const res: any = await getTeaDataAPI() //获取茶数据
//   // const res: any = await getpackagesDataAPI() //获取包数据
//   // const res: any = await getclothesDataAPI() //获取衣服数据
//   // const res: any = await getshoesDataAPI() //获取鞋子数据
//   // const res: any = await getlangDataAPI() //获取郎酒数据
//   // const res: any = await getmilkDataAPI() //获取牛奶数据
//   // const res: any = await getLiBangDataAPI() //获取立邦数据
//   console.log(res.data)
//   let lists = []
//   for (let key in res.data) {
//     console.log(res.data[key])

//     lists.push({ code: res.data[key].code })
//   }
//   console.log(lists)

//   dataLists.value = lists.map((item: any) => {
//     // item.add('isReplacing', false)
//     return { ...item, isReplacing: false }
//   })
// }
// getData()

// watch(
//   () => useWebsocketStore().date,
//   () => {
//     getData()
//   },
// )

const items = ref(new Array())
const nextId = ref(11)

// watch(
//   () => yanZhengStore.code,
//   () => {
//     removeItem(yanZhengStore.code)
//   },
// )

function removeItem(item: any) {
  console.log('item', item)
  const index = dataLists.value.findIndex((i) => i.code == item[1])
  let data = { ...dataLists.value[index] }
  data.code = item[0]
  yanZhengStore.data = data
  console.log('找到了', index)
  if (index !== -1) {
    // 标记为正在替换
    dataLists.value[index].isReplacing = true

    setTimeout(() => {
      // 移除旧项，并在同一位置添加新项
      dataLists.value.splice(index, 1, data)
      nextId.value++
    }, 500) // 确保这个延时与.fade-leave-active的持续时间一致
  }
}

const inter = ref()
onMounted(() => {})
onUnmounted(() => {
  clearInterval(inter.value)
})

//#endregion

// function openVideo() {
//   useVideoStore().date = Math.random()
//   console.log(Math.random())
// }
</script>

<style lang="scss" scoped>
#productID {
  height: 90vh;
  width: 100vw;
  position: relative;
  font-size: 18px;
  .tableContentClass {
    position: absolute;
    top: 8.7rem;
    left: 2.4rem;
    margin-top: 30px;
    background: #0000;
    width: 22vw;
    height: 43vh;
    overflow-y: scroll;
    // overflow-y: hidden;
    &::-webkit-scrollbar {
      display: none;
    }

    ul {
      padding-inline-start: 0 !important;
    }

    .tableClass {
      margin: 5px 0 !important;
      padding: 0 1vw !important;
      // background: url('../../static/images/Small_content_box.png') no-repeat;
      // background-size: 100% auto;
      // background-position: calc(50%);
      border-bottom: 1px #fff dashed;
      height: 3.5vh;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;

      li {
        color: #0ff;
        // height: 100%;
        display: inline-block;
        &:nth-child(2n) {
          width: 90% !important;
          color: #fff;
        }
        &:nth-child(2n-1) {
          width: 10% !important;
        }
      }
    }

    .pageClass {
      position: fixed;
      top: 39.2rem;
      left: 12.5rem;
    }

    // :deep(.uni-table) {
    //   background-color: #fff0 !important;
    //   height: 520px;

    //   .uni-table-tr {
    //     margin: 5px 0 !important;
    //     background: url('../../static/images/Small_content_box.png') no-repeat;
    //     background-size: 100% auto;
    //     background-position: calc(50%);

    //     .uni-table-th,
    //     .uni-table-td {
    //       color: #0ff !important;
    //       border: none !important;
    //       box-sizing: border-box;
    //       // border-left: 1px #fff solid !important;
    //       // margin: 10px 0 !important;
    //       padding: 6px 10px !important;
    //       text-align: center;
    //       align-items: center;
    //     }

    //     .uni-table-td {
    //       .tdClass {
    //         height: 40px !important;
    //         display: flex;
    //         justify-content: center;
    //         align-items: center;
    //       }
    //     }

    //   }
    // }
  }
  .dataContentClass {
    position: absolute;
    top: 8.7rem;
    right: 1.3rem;
    width: 24vw;
    height: 45vh;
    color: #fff;
    font-size: 24px;
    font-weight: 700;
    .shengchan,
    .hexiao {
      position: absolute;
      top: 4.3rem;
      right: 8.3rem;
      width: 7vw;
      height: 7vh;
      div {
        &:nth-child(2n-1) {
          text-align: center;
          text-align-last: justify;
        }
        &:nth-child(2n) {
          margin-top: 10px;
          text-align: left;
          font-size: 20px;
        }
      }
    }
    .hexiao {
      position: absolute;
      top: 12.7rem;
    }
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0.1;
}
</style>
